package com.example.utils;

import com.example.framework.GroupData;
import com.thoughtworks.xstream.XStream;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class GroupDataGenerator {
    private static TestDataGenerator dataGen =  new TestDataGenerator();

    public static void main(String[] args) throws IOException {
        if (args.length < 3) {
            System.out.println("Incorrect parameters. Should be 3 parameters: amount of test data, file, file format.");
            return;
        }

        int amount = Integer.parseInt(args[0]);
        File file = new File(args[1]);
        String format = args[2];

        if (file.exists()){
            System.out.println("File " + file + "exists. Please use another name for file or remove existent file.");
            return;
        }

        List<GroupData> groups = generateRandomGroups(amount);

        if (format.equals("csv")) {
            saveGroupsToCsvFile(groups, file);
        } else if (format.equals("xml")) {
            saveGroupsToXmlFile(groups, file);
        } else {
            System.out.println("Unknown file format: " + format + ".\nPlease specify correct file format: csv or xml.");
            return;
        }
    }

    public static void saveGroupsToXmlFile(List<GroupData> groups, File file) throws IOException {
        XStream xstream = new XStream();
        xstream.alias("group", GroupData.class);
        String xml = xstream.toXML(groups);
        FileWriter writer = new FileWriter(file);
        writer.write(xml);
        writer.close();
    }

    public static List<GroupData> loadGroupsFromXmlFile(File file) {
        List<GroupData> list = new ArrayList<GroupData>();
        XStream xstream = new XStream();
        xstream.alias("group", GroupData.class);
        list = (List<GroupData>) xstream.fromXML(file);
        return list;
    }

    private static void saveGroupsToCsvFile(List<GroupData> groups, File file) throws IOException {
        FileWriter writer = new FileWriter(file);
        for (GroupData group : groups){
            writer.write(group.getName() + "," +group.getHeader() + "," + group.getFooter() + ",#" + "\n");
        }
        writer.close();
    }

    public static List<GroupData> loadGroupsFromCsvFile(File file) throws IOException {
        List<GroupData> list = new ArrayList<GroupData>();
        FileReader reader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = bufferedReader.readLine();
        while (line != null){
            String[] groupParameters = line.split(",");
            GroupData group = new GroupData()
                    .withName(groupParameters[0])
                    .withHeader(groupParameters[1])
                    .withFooter(groupParameters[2]);
            list.add(group);
            line = bufferedReader.readLine();
        }
        bufferedReader.close();
        return list;
    }

    public static List<GroupData> generateRandomGroups(int amount) {
        List<GroupData> list = new ArrayList<GroupData>();

        for (int i = 0; i < amount; i++) {
            GroupData group = new GroupData()
                    .withName(dataGen.getRandomStringWithRandomLength(20))
                    .withHeader(dataGen.getRandomStringWithRandomLength(20))
                    .withFooter(dataGen.getRandomStringWithRandomLength(20));
            list.add(group);
        }
        return list;
    }
}
